package relatorio.servlets;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.ImageIcon;

import net.sf.jasperreports.engine.JRException;
import relatorio.jdbc.Conexao;
import relatorio.util.ReportUtils;

/**
 * Servlet implementation class Ficha_Inscricao
 */
public class Ficha_Inscricao extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Ficha_Inscricao() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request,response);
	}
	
	
	@SuppressWarnings("unchecked")
	protected void processRequest(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException { 
		OutputStream out = null;		
		// obtém o relatório compilado
		InputStream inputStream = getClass().getResourceAsStream("/relatorio/pdf/inscricao.jasper");
		// preenche o mapa de parâmetros
		Map parametros = new HashMap();	
		ImageIcon img = new ImageIcon(getClass().getResource("/relatorio/logo/logo.jpg")); 
		parametros.put("logo", img.getImage());
		int id=Integer.parseInt(request.getParameter("id_aluno"));
		parametros.put("aluno_id",new Integer(id));		
		try {
			// gera o relatório e atribui o OutputStream gerado
		    out = ReportUtils.createPDFReport(inputStream,parametros,Conexao.getConnection(),response);
		} catch ( SQLException exc ) {
			exc.printStackTrace();
		} catch ( JRException exc ) {
			exc.printStackTrace();
		} finally {
		    // se não aconteceu nenhum problema, fecha o output stream
		    if ( out != null ) {
		    	out.close();
		    }
		}	
	}
}
